/*---------------------------
|+++++++++++++++++++++++++++|			
|	   Nguyen Duc Tai		|
|+++++++++_MUTEX_H_+++++++++|
---------------------------*/

#include "Mutex.h"
#include <iostream>

using namespace std;

Mutex::~Mutex()
{
	pthread_mutex_destroy( &this->mutex_count );
	pthread_cond_destroy( &this->cond_count );

}

//init mutex
void Mutex::init()
{
	pthread_mutex_init( &this->mutex_count, NULL );
	pthread_cond_init( &this->cond_count, NULL);
}

//lock mutex
void Mutex::lock()
{
	pthread_mutex_lock( &this->mutex_count );
}

//unlock mutex
void Mutex::unlock()
{
	pthread_mutex_unlock( &this->mutex_count );
}

//block the calling thread, wait for the signal
void Mutex::wait()
{
	pthread_cond_wait( &this->cond_count, &this->mutex_count );
}

//signal another thread which is waiting on condition 
void Mutex::signal()
{
	pthread_cond_signal( &this->cond_count );
}